package com.annie.boots.controller;

import com.annie.boots.bean.Product;
import com.annie.boots.service.ProductService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class ProductController {
    @Autowired
    ProductService productService;
    @ResponseBody
    @GetMapping("/product")
    public Product getProduct(@RequestParam("id") Integer id){
        return productService.getProductById(id);
    }
    @GetMapping("/productList")
    public String getAllList(Model model){
        //List<Product> list = productService.getListByName(null);
        model.addAttribute("pList",productService.getListByName(null));
        return "table/list";
    }

    @PostMapping("/productList")
    public String toList(@RequestParam(value = "productName",required = false) String productName,
                         Model model){
        model.addAttribute("pList",productService.getListByName(productName));
        return "table/list";
    }

    @GetMapping("/li")
    public String toList(){
        return "table/plist";
    }

    /**
     * 查询
     * @param currentPage 当前页
     * @param pageSize  每页显示数量
     * @param productName  商品名称
     * @return
     */
    @ResponseBody
    @GetMapping("/testPageHelper1")
    public PageInfo<Product> testPageHelper1(@RequestParam(defaultValue="1")Integer currentPage,
                                             @RequestParam(defaultValue="10")Integer pageSize,
                                             @RequestParam(value = "productName",required = false) String productName){
        PageInfo<Product> queryResult = productService.findAllProductByPageS(productName,currentPage, pageSize);
        return queryResult;
    }

    /**
     * 删除操作
     * @param id  商品ID
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/product/del")
    public Map delete(@RequestParam("id") Integer id) throws Exception {
        boolean b = productService.delProduct(id);
        Map<String,Object> map = new HashMap<>();
        map.put("flag",b);
        return map;
    }

    /**
     * 进入到添加页
     * @return
     */
    @GetMapping("/padd")
    public String paddPage(){
        return "table/padd";
    }

    /**
     * 添加到数据库
     * @param product
     * @return
     */
    @ResponseBody
    @PostMapping("/addProduct")
    public Map saveProduct(Product product){
        boolean b = productService.addProduct(product);
        Map<String,Object> map = new HashMap<>();
        map.put("flag",b);
        return map;
    }

    /**
     * 主页的欢迎页面显示
     * @return
     */
    @GetMapping("/welcome.html")
    public String welcome(){
        return "welcome";
    }

    /**
     * 进入到编辑页面
     * @return
     */
    @GetMapping("/edit.html")
    public String edit(){
        return "table/pedit";
    }

    /**
     * 根据ID查出页面信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/getProductById")
    public Product getProductByIds(@RequestParam("id") Integer id){
        Product product = productService.getProductById(id);
        return product;
    }

    /**
     * 修改操作到数据库
     * @param product
     * @return
     */
    @ResponseBody
    @PostMapping("/editProduct")
    public Map editProducts(Product product){
        boolean b = productService.updateProduct(product);
        Map<String,Object> map = new HashMap<>();
        map.put("flag",b);
        return map;
    }

}
